import {Injectable} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {Observable} from 'rxjs/Observable';
import {Newsmodle} from './newsmodle';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class NewsService {

    url_newslist = 'News/list';
    url_delnews = 'News/del';
    url_shownews = 'News/show/';
    url_about = 'News/about';
    url_add = 'News/add';

    constructor(private http: _HttpClient) {
    }

    getNewslistByType(type: string, page: number): Observable<Newsmodle> {
        return this.http.get<Newsmodle>(this.url_newslist + '/' + type + '/' + page);
    }


    delNewsById(id: number) {
        return this.http.delete(this.url_delnews, {id: id});
    }


    showNewsById(id: string): Observable<string> {
        // return this.http.get(this.url_shownews + '' + id.toString());
        return this.http.get(this.url_shownews + id);
    }

    getAbout() {
        return this.http.get(this.url_about);
    }

    addNews(item): Observable<string> {

        const xx = {
            id: item.id ? item.id : 0,
            title: item.title,
            content: item.content,
            img: item.img ? item.img : '',
            type: item.type,
            subcontent: item.subconent ? item.subconent : '',
        };
        // console.error(JSON.stringify(xx) + '--------');

        return this.http.post<string>(this.url_add, JSON.stringify(xx));
    }
}
